<?php

class LoginController extends Controller
{

	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('view','inbox'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('index'),
                'users'=>array('@'),
            ),
			array('deny',  // deny all users
				'actions'=>array('index'),
				'users'=>array('*'),
			),
		);
	}	
	public function actionIndex()
	{
		ob_start();
		if (isset(Yii::app()->user->id)) {
			$this->redirect(Yii::app()->user->returnUrl);
			exit;
		}
		$model=new UserLogin;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['UserLogin']))
		{
			$model->attributes=$_POST['UserLogin'];
			// validate user input and redirect to the previous page if valid
			if($model->validate()){
                if(Yii::app()->user->checkAccess('admin')){
                    $this->redirect(Yii::app()->request->hostInfo.('/myjoco/backend'));
                }else{
                    $this->redirect(Yii::app()->user->returnUrl);
                }
			    
			}
				
		}
		// display the login form
		$this->render('index',array('model'=>$model));
        ob_end_flush();
	}
    
    public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}